package com.oa.entities;

import com.oa.domain.UndeleteEntity;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "oa_user")
public class User extends UndeleteEntity<Long> {
    /**
     *
     */
    private static final long serialVersionUID = 4330840932398620078L;

    /** 用户名 **/
    public static final String USER_USERNAME="username";

    /** 邮件 **/
    public static final String USER_EMAIL="email";

    /** 电话号码 **/
    public static final String USER_PHONE="phone";

    public static final String PERSON_LOGIN_KEY="person_login_key";

    public static final String USER_COOKIE_NAME="admincookienane";

    /** 用户名 **/
    private String username;

    /** 密码 **/
    private String password;

    /** 邮件 **/
    private String email;

    /** 手机号码**/
    private String phone;

    /** 登录时间 **/
    private Long loginTime;

    /** 最后登录ip地址 **/
    private String loginIp;

    /** 用户详情 **/
    private UserInfo info;

    private Set<Role> roles= new HashSet<>();

    /** 是否是管理员 **/
    private Boolean isManager;

    private Long createTime;

    private String salt;

    @NotNull
    @Column(name = "username",nullable = false,unique = true)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @NotNull
    @Length(max = 64)
    @Column(name = "password",nullable = false,length = 64)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Length(max = 64)
    @Column(name = "email",length = 64)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Column(name = "phone")
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Column(name = "login_time")
    public Long getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Long loginTime) {
        this.loginTime = loginTime;
    }

    @Column(name = "login_ip")
    public String getLoginIp() {
        return loginIp;
    }

    public void setLoginIp(String loginIp) {
        this.loginIp = loginIp;
    }

    @Override
    @Id
    @Column(nullable = false,unique = true,updatable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return super.getId();
    }

    @Override
    public void setId(Long id) {
        super.setId(id);
    }

    @OneToOne(fetch = FetchType.LAZY,mappedBy = "user",cascade = CascadeType.ALL,orphanRemoval = false)
    public UserInfo getInfo() {
        return info;
    }

    public void setInfo(UserInfo info) {
        this.info = info;
    }

    /**
     * 获取角色
     *
     * @return 角色
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "oa_user_role")
    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    @Column(name = "user_manage",nullable = false,columnDefinition = "boolean default false")
    public Boolean getManager() {
        return isManager;
    }

    public void setManager(Boolean manager) {
        isManager = manager;
    }

    @Column(name = "create_time",nullable = false)
    public Long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Long createTime) {
        this.createTime = createTime;
    }

    @Column(name = "user_salt")
    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    @Transient
    public String getCredentialsSalt(){
        return username+salt;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", email='" + email + '\'' +
                ", phone='" + phone + '\'' +
                ", loginTime=" + loginTime +
                ", loginIp='" + loginIp + '\'' +
                ", isManager=" + isManager +
                ", createTime=" + createTime +
                ", salt='" + salt + '\'' +
                '}';
    }
}
